# -*- coding: utf-8 -*-

'''
Created on Apr 3, 2013

@author: LONG HOANG GIANG
'''

import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
import pyLib
import json
import re
import gzip
from urlparse import urljoin
import traceback
import logging

OUTPUT = "/longhoanggiang/truyentext/"

def getDetail(item):
    tree = pyLib.loadWeb(item['url']).build_tree()
    contentNode = tree.xpath("//div[@id='main']/div[@class='entry']")[0]
    html = pyLib.cleanHTML(pyLib.Etree.tostring(contentNode), ['b', 'h4'])
    print html
    html = re.sub(r"\n", "<br />", html)
    return html

def process(url):
    folder = pyLib.extractText("doctruyen360\.com/([^/]+)/*", url, 1, "untitled")
    print folder
    pyLib.createIfNotExistsPath(OUTPUT + folder + "/files/")
    tree = pyLib.loadWeb(url).build_tree()
    data = []
    for xpathStr in ["//div[@class='entry']/table[1]//td//a", "//div[@class='entry']//ul/li/a", "//div[@class='entry']//p/a"]:
        nodes = tree.xpath(xpathStr)
        if len(nodes) == 0: continue
        for item in nodes:
            try:
                title = getTitle(pyLib.stringify(item))
                link = item.get('href', '')
                if link == '': continue
                link = urljoin(url, link)
                print '-------------------------------------------------------------'
                print title, link
                print '-------------------------------------------------------------'
                fileName = pyLib.crc32unsigned(link) 
                detail = getDetail({'name': title, 'path': fileName, 'url': link})
                detail = '''<b>{0}</b><br /><hr /><br />{1}'''.format(title, detail)
                pyLib.gzip(OUTPUT + folder + "/files/" + fileName, json.dumps(detail))
                data.append({'name': title, 'path': fileName})
            except:
                traceback.print_exc()
    if len(data) > 0:
        data.reverse()
        pyLib.gzip(OUTPUT + folder + "/data", pyLib.encryptCipher(json.dumps(data)))

def merge(source, dest):
    if source.endswith("/"): source = os.path.dirname(source)
    if dest.endswith("/"): dest = os.path.dirname(dest)
    fp = gzip.open(source + "/data", "rb")
    data = json.loads(fp.read())
    fp.close()
    
    fp = gzip.open(dest + "/data", "rb")
    datadest = json.loads(fp.read())
    fp.close()
    
    for item in data:
        print item['title']
        datadest.append(item)
    pyLib.gzip(dest + "/data", json.dumps(datadest))
    

def getTitle(string):
    if not originalTitle:
        c = string
        if re.search(r"–", string):
            string = pyLib.extractText(r"[^–]+–(.+)", string, 1, c).strip()
    return pyLib.toUpper(string)

if __name__ == '__main__':
    
    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt='%d/%m/%Y %H:%M:%S')
    
    url = raw_input("Enter URL: ")
    originalTitle = raw_input("Keep original title ?: ")
    if originalTitle.lower() in ['yes', 'y', 'true']:
        originalTitle = True
    else:
        originalTitle = False
    process(url)
    
#    merge("/longhoanggiang/truyentext/doc-full-truyen-anh-nhat-dinh-lam-em-yeu-anh-phan-ii", "/longhoanggiang/truyentext/doc-truyen-anh-nhat-dinh-lam-em-yeu-anh-full")
    
    logging.info("finished")
    os._exit(1)
    